// pages/productsales/productsales.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shequan: ['快团团', '群接龙'],
        dianshang: ['京东', '淘宝'],
        addshequn: false,
        addshowname: '',
        adddataping: [],
        shili: ['https://bkimg.cdn.bcebos.com/pic/4a36acaf2edda3cc5…s=image/resize,m_lfit,w_536,limit_1/format,f_auto'],

        adddianshang: false,
        adddianname: '',
        adddatadian: [],
    },
    onClose() {
        this.setData({
            addshequn: false,
            addshowname: '',
            adddianshang: false,
            adddianname: '',
        })
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 300);
    },
    shequnshow() {
        this.setData({
            addshequn: true,
        })
    },
    onBeforeClose(action, done) {
        console.log(action)
        if (action === "confirm") {
            return done(false);
        } else {
            return done();
        }
    },
    addpingtai() {
        if (this.data.addshowname != "") {
            let newshequan = this.data.shequan
            newshequan.push(this.data.addshowname)
            console.log(newshequan)
            this.setData({
                shequan: newshequan,
                addshowname: ''
            })
        } else {
            wx.showToast({
                title: '还未输入,请输入！',
                icon: 'none',
                duration: 2000
            })
            return false
        }
    },

    adddatapingtai(items) {
        let array = {
            appName: items.currentTarget.dataset.select,
            // 名字
            salesVolume: '',
            // 销量
            "channelType": 1,
            image: [],
            qrCode: [],
            linkAddress: '',
        }
        for (const aa in this.data.adddataping) {
            if (this.data.adddataping[aa].appName == items.currentTarget.dataset.select) {
                wx.showToast({
                    title: '已经添加',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
            
        }
        let newlist = this.data.adddataping
        newlist.push(array)
        this.setData({
            adddataping: newlist
        })
    },

    bangafterReadbang(event) {
        console.log(event)
        let setdatas = `adddataping[${event.currentTarget.dataset.select}].image`
        const { file } = event.detail;
        const filebangList = this.data.adddataping[event.currentTarget.dataset.select].image;
        filebangList.push(...file);
        this.setData({
            [setdatas]: filebangList
        });
    },
    bangdelbang(event) {
        let setdatas = `adddataping[${event.currentTarget.dataset.select}].image`
        let filebangList = this.data.adddataping[event.currentTarget.dataset.select].image;;
        filebangList.splice(event.detail.index, 1)
        this.setData({
            [setdatas]: filebangList
        });
    },

    shangafterReadbang(event) {
        console.log(event)
        let setdatas = `adddataping[${event.currentTarget.dataset.select}].qrCode`
        const { file } = event.detail;
        const filebangList = this.data.adddataping[event.currentTarget.dataset.select].qrCode;
        filebangList.push(...file);
        this.setData({
            [setdatas]: filebangList
        });
    },
    shangdelbang(event) {
        let setdatas = `adddataping[${event.currentTarget.dataset.select}].qrCode`
        let filebangList = this.data.adddataping[event.currentTarget.dataset.select].qrCode;;
        filebangList.splice(event.detail.index, 1)
        this.setData({
            [setdatas]: filebangList
        });
    },
    itemxiao(items) {
        let setdatas = `adddataping[${items.currentTarget.dataset.select}].salesVolume`
        this.setData({
            [setdatas]: items.detail
        });
    },
    itemlink(items) {
        let setdatas = `adddataping[${items.currentTarget.dataset.select}].linkAddress`
        this.setData({
            [setdatas]: items.detail,
        });
    },
    delitem(items) {
        console.log(items)
        let setdatas = this.data.adddataping
        setdatas.splice(items.currentTarget.dataset.select, 1)
        this.setData({
            adddataping: setdatas
        });
    },


    pingtaishow() {
        this.setData({
            adddianshang: true
        })
    },
    adddianshangpingtai() {
        if (this.data.adddianname != "") {
            let newdianshang = this.data.dianshang
            newdianshang.push(this.data.adddianname)
            console.log(newdianshang)
            this.setData({
                dianshang: newdianshang,
                adddianname: ''
            })
            this.onClose()
        } else {
            wx.showToast({
                title: '还未输入,请输入！',
                icon: 'none',
                duration: 2000
            })
            return false
        }
    },
    adddatadianshag(items) {
        let array = {
            appName: items.currentTarget.dataset.select,
            // 名字
            salesVolume: '',
            // 销量
            "channelType": 2,
            image: [],
            qrCode: [],
            linkAddress: '',
        }
        for (const aa in this.data.adddatadian) {
            if (this.data.adddatadian[aa].appName == items.currentTarget.dataset.select) {
                wx.showToast({
                    title: '已经添加',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
            
        }
        let newlist = this.data.adddatadian
        newlist.push(array)
        this.setData({
            adddatadian: newlist
        })
    },
    delitemping(items) {
        console.log(items)
        let setdatas = this.data.adddatadian
        setdatas.splice(items.currentTarget.dataset.select, 1)
        this.setData({
            adddatadian: setdatas
        });
    },

    bangafterReadbang2(event) {
        console.log(event)
        let setdatas = `adddatadian[${event.currentTarget.dataset.select}].image`
        const { file } = event.detail;
        const filebangList = this.data.adddatadian[event.currentTarget.dataset.select].image;
        filebangList.push(...file);
        this.setData({
            [setdatas]: filebangList
        });
    },
    bangdelbang2(event) {
        let setdatas = `adddatadian[${event.currentTarget.dataset.select}].image`
        let filebangList = this.data.adddatadian[event.currentTarget.dataset.select].image;;
        filebangList.splice(event.detail.index, 1)
        this.setData({
            [setdatas]: filebangList
        });
    },

    shangafterReadbang4(event) {
        console.log(event)
        let setdatas = `adddatadian[${event.currentTarget.dataset.select}].qrCode`
        const { file } = event.detail;
        const filebangList = this.data.adddatadian[event.currentTarget.dataset.select].qrCode;
        filebangList.push(...file);
        this.setData({
            [setdatas]: filebangList
        });
    },
    shangdelbang4(event) {
        let setdatas = `adddatadian[${event.currentTarget.dataset.select}].qrCode`
        let filebangList = this.data.adddatadian[event.currentTarget.dataset.select].qrCode;;
        filebangList.splice(event.detail.index, 1)
        this.setData({
            [setdatas]: filebangList
        });
    },
    itemxiao2(items) {
        let setdatas = `adddatadian[${items.currentTarget.dataset.select}].salesVolume`
        this.setData({
            [setdatas]: items.detail
        });
    },
    itemlink2(items) {
        let setdatas = `adddatadian[${items.currentTarget.dataset.select}].linkAddress`
        this.setData({
            [setdatas]: items.detail,
        });
    },

    backsetdata() {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            "sqtg": this.data.adddataping,
            "dsxl": this.data.adddatadian,
        })
        setTimeout(() => {
            this.naveback()
        }, 600);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            "adddataping": JSON.parse(options.sqtg),
            "adddatadian": JSON.parse(options.dsxl),
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})